package com.dgcsxy.my_college_note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgcsxy.my_college_note.common.utils.RedisUtil;
import com.dgcsxy.my_college_note.common.utils.SnowFlakeIdWorker;
import com.dgcsxy.my_college_note.entity.dto.user.UserInfoDto;
import com.dgcsxy.my_college_note.entity.vo.user.UserLoginVo;
import com.dgcsxy.my_college_note.entity.vo.user.UserRegisterVo;
import com.dgcsxy.my_college_note.entity.vo.user.UserUpdateVo;
import com.dgcsxy.my_college_note.entity.pojo.McnUser;
import com.dgcsxy.my_college_note.mapper.McnUserMapper;
import com.dgcsxy.my_college_note.service.McnUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-10-24
 */
@Service
public class McnUserServiceImpl extends ServiceImpl<McnUserMapper, McnUser> implements McnUserService {

    private final McnUserMapper mcnUserMapper;
    private final RedisUtil redisUtil;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final SnowFlakeIdWorker snowFlakeIdWorker;

    public McnUserServiceImpl(McnUserMapper mcnUserMapper,
                              RedisUtil redisUtil,
                              BCryptPasswordEncoder bCryptPasswordEncoder,
                              SnowFlakeIdWorker snowFlakeIdWorker) {
        this.mcnUserMapper = mcnUserMapper;
        this.redisUtil = redisUtil;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.snowFlakeIdWorker = snowFlakeIdWorker;
    }

    @Override
    public String register(UserRegisterVo userRegisterDto) {
        String email = userRegisterDto.getUserEmail();
        String redisCaptcha = (String) redisUtil.get("app" + email.trim());
        if (!redisCaptcha.equals(userRegisterDto.getCode())) {
            return "验证码不正确";
        }
        if (redisCaptcha == null) {
            return "验证码不为空";
        }
        McnUser user_enter = mcnUserMapper.selectOne(new QueryWrapper<McnUser>()
                .eq("user_email", userRegisterDto.getUserEmail()));
        if (user_enter != null) {
            return "用户已注册";
        }
        McnUser mcnUser = new McnUser();
        mcnUser.setUserId(String.valueOf(snowFlakeIdWorker.nextId()));
        mcnUser.setUserAvator("https://mycollegenote.oss-cn-hangzhou.aliyuncs.com/avator/avator.png");
        mcnUser.setMainSkin("https://mycollegenote.oss-cn-hangzhou.aliyuncs.com/skin/skin.jpg");
        mcnUser.setUserEmail(userRegisterDto.getUserEmail());
        mcnUser.setUserPassword(bCryptPasswordEncoder.encode(userRegisterDto.getUserPassword()));
        mcnUser.setUserName("默认用户名");
        mcnUser.setUserSignature("大学很忙，苦中作乐");
        mcnUser.setUserStatus(1);
        int i = mcnUserMapper.insert(mcnUser);
        redisUtil.del("app" + email.trim());
        return i > 0 ? "注册成功" : "注册失败";
    }

    @Override
    public String login(UserLoginVo loginDto) {
        McnUser user = mcnUserMapper.selectOne(new QueryWrapper<McnUser>()
                .eq("user_email", loginDto.getUserEmail()));
        if (user == null) {
            return "不存在此用户";
        }
        boolean matches = bCryptPasswordEncoder.matches(loginDto.getUserPassword(), user.getUserPassword());
        if (!matches) {
            return "用户账号密码错误";
        }
        return matches == true ? user.getUserId() : "登录失败";
    }

    @Override
    public String updateUser(UserUpdateVo userUpdateDto) {
        McnUser mcnUser = new McnUser();
        mcnUser.setUserEmail(userUpdateDto.getUserEmail());
        mcnUser.setUserName(userUpdateDto.getUserName());
        mcnUser.setUserAvator(userUpdateDto.getUserAvator());
        mcnUser.setMainSkin(userUpdateDto.getMainSkin());
        mcnUser.setUserSignature(userUpdateDto.getUserSignature());
        int count = mcnUserMapper.update(mcnUser, new QueryWrapper<McnUser>()
                .eq("user_email", mcnUser.getUserEmail()));
        return count > 0 ? "修改成功" : "修改失败";
    }

    @Override
    public UserInfoDto selectUserByEmail(String userEmail) {
        McnUser mcnUser = mcnUserMapper.selectOne(new QueryWrapper<McnUser>()
                .eq("user_email", userEmail));
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUserName(mcnUser.getUserName());
        userInfoDto.setUserAvator(mcnUser.getUserAvator());
        userInfoDto.setUserSignature(mcnUser.getUserSignature());
        userInfoDto.setMainSkin(mcnUser.getMainSkin());
        return userInfoDto;
    }

}
